/* ==================================================================== *
 *
 * This file is part of Mapics Framework.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author Andrey Cherkashin <action.open@gmail.com>
 *
 * ==================================================================== */

/**
 * Constructs overlay for yandex map layer
 *
 * @param m_Level    (int) Level of this overlay
 */
mapics.MapLayerYandexOverlay = function(level)
{
    /**
     * Adding overlay div in map container
     */
    this.m_Div = this.createDiv(level);

    /**
     * List of map objects
     */
    this.m_MapObjects = [];
};

/**
 * Inheritance from mapics.MapLayerOverlay
 */
mapics.MapLayerYandexOverlay.prototype = new mapics.MapLayerOverlay();

/** ------------------------------------------------------------------------- **
 *  ACTIONS
 ** ------------------------------------------------------------------------- **/

/**
 * Updates this map overlay objects
 *
 * @return mapics.MapLayerYandexOverlay
 */
mapics.MapLayerYandexOverlay.prototype.update = function() {
    this.onMapUpdate();
};

/** ------------------------------------------------------------------------- **
 *  EVENTS
 ** ------------------------------------------------------------------------- **/

/**
 * Initializes when overlay is added on map
 *
 * @param pMap            (YMaps.Map) Yandex Map pointer
 * @param parentContainer (HTMLObject) Div that will contains this overlay
 */
mapics.MapLayerYandexOverlay.prototype.onAddToMap = function(pMap, parentContainer)
{
    this.m_Map = pMap;
    this.m_Div.style.display = "block";
    parentContainer.appendChild(this.m_Div);
};

/**
 * Initializes when overlay is removed from map
 */
mapics.MapLayerYandexOverlay.prototype.onRemoveFromMap = function()
{
    this.m_Div.style.display = "none";
    this.m_Div.parentNode.removeChild(this.m_Div);
};

/**
 * Initializes when overlay is drawing
 */
mapics.MapLayerYandexOverlay.prototype.onMapUpdate = function()
{
    for(var i = 0, max = this.m_MapObjects.length; i < max; i++)
    {
        var pos = this.m_Map.converter.coordinatesToMapPixels(
            this.m_MapObjects[ i ].getPosition("yandex")
        );

        if( (offset = this.m_MapObjects[ i ].getOffset()) ) {
            pos.x += offset.x;
            pos.y += offset.y;
        }

        this.m_MapObjects[ i ].getDiv().style.left = pos.x + "px";
        this.m_MapObjects[ i ].getDiv().style.top  = pos.y + "px";
    }
};